package com.panda.utils;



import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.EventQueue;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.RandomAccessFile;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class APP {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					APP window = new APP();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public APP() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 659, 307);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Panel panel = new Panel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		
		Label label = new Label("\u6587\u4EF6\u5217\u8868");
		
		final TextArea textArea = new TextArea();
		textArea.setEditable(false);
		
		Button button = new Button("\u9009\u62E9\u6587\u4EF6");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc = new JFileChooser();
				jfc.setMultiSelectionEnabled(true);
				jfc.showOpenDialog(null);
				File[] files = jfc.getSelectedFiles();
				String path="";
				for(File f:files){
					path+=f.getAbsolutePath()+"\n";
				}
				textArea.setText(path);
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(33)
					.addComponent(label, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(5)
					.addComponent(textArea, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(5)
					.addComponent(button, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(78)
					.addComponent(label, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(5)
					.addComponent(textArea, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(78)
					.addComponent(button, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
		);
		panel.setLayout(gl_panel);
		
		Panel panel_1 = new Panel();
		frame.getContentPane().add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(null);
		
		Panel panel_2 = new Panel();
		panel_2.setBounds(102, 10, 229, 33);
		
		Label label_1 = new Label("\u5BC6\u7801\uFF1A");
		panel_2.add(label_1);
		
		final TextField textField = new TextField();
		textField.setColumns(20);
		panel_2.add(textField);
		panel_1.add(panel_2);
		
		Button button_1 = new Button("\u52A0\u5BC6");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String password=textField.getText();
				if(password.trim().equals("")) return;
				String[] file=textArea.getText().split("\n");
				RandomAccessFile raf;
				try {
				for(String path:file){
					raf = new RandomAccessFile(path,"rw");
					byte[] datas=new byte[102400];
					raf.read(datas,0,datas.length);
					encrypt(datas,password);
					raf.seek(0);
					raf.write(datas,0,datas.length);
				}
				} catch(Exception e1) {
					e1.printStackTrace();
				}
				textArea.setText("���ܳɹ���");
			}
		});
		button_1.setBounds(350, 20, 76, 23);
		panel_1.add(button_1);
		Button button_2 = new Button("\u89E3\u5BC6");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String password=textField.getText();
				if(password.trim().equals("")) return;
				String[] file=textArea.getText().split("\n");
				RandomAccessFile raf;
				try {
				for(String path:file){
					raf = new RandomAccessFile(path,"rw");
					byte[] datas=new byte[102400];
					raf.read(datas,0,datas.length);
					encrypt(datas,password);
					raf.seek(0);
					raf.write(datas,0,datas.length);
				}
				} catch(Exception e1) {
					e1.printStackTrace();
				}
				textArea.setText("���ܳɹ�");
			}
		});
		button_2.setBounds(468, 20, 76, 23);
		panel_1.add(button_2);
	}
 
	public  byte[] encrypt(byte[] source,String password) throws Exception{
		byte[] miya=password.getBytes();
		int mlenth=miya.length;
		for(int i=0;i<source.length;i++){
			source[i]=(byte)(source[i]^miya[i%mlenth]);
		}
		return source;
	}
}
